import json
import random

import pymysql



def mydb(sql):
    # 建立数据连接
    db = pymysql.connect(
        user='test',  # 用户名
        password='test123',  # 密码
        host='192.168.0.125',  # 数据库ip
        port=3306,  # 数据库端口
        database="userinfo",  # 数据库名称
        charset='utf8mb4')  # 字符编码
    # 建立游标 (游标的作用：执行SQL语句，获取查询结果)
    cs = db.cursor()
    # 2、通过 游标 执行SQL
    cs.execute(sql)
    # 3、获取查询的结果，如果执行的不是 查询语句，返回（）
    data = cs.fetchall()  # 以元组方式返回查询结果 ， ((第一条数据),(第二条数据)  )
    # 对数据进行了 增删改 需要 commit 提交 或者 rollback 撤销、回滚
    db.commit()
    # 关闭连接
    db.close()
    return data



def check_pwd(pwd):
    if len(pwd) <= 8:
        return False
    else:
        a = b = c = d = 0
        for i in password:
            if i.isdigit():
                a = 1
            elif i.isupper():
                b = 1
            elif i.islower():
                c = 1
            else:
                d = 1
        if a + b + c + d < 3:
            return False
        else:
            return True



import time



def make_userid():
    uid = int(time.time())
    before = random.randint(11, 99)
    after = random.randint(10, 99)
    uid = f"{before}{uid}{after}"
    return uid



print('欢迎来到爱坤系统，请输入您的操作指令')
print('进入注册请输入：1')
print('进入登录请输入：2')
print('进入修改密码请输入：3')
print('进入删除用户请输入：4')
print('退出系统请输入：0')
# 读取数据
# f = open('data.json', 'r', encoding='utf-8')
# data = json.load(f)
# f.close()



while True:
    shu = input('请输入指令')
    # 退出程序
    if shu == '0':
        break
    # 注册
    elif shu == '1':
        username = input("请输入用户名：")
        if 1 <= len(username) <= 20:
            data = mydb(f"select * from user where uname = '{username}';")
            if len(data) == 0:
                password = input("请输入密码：")
                if check_pwd(pwd=password):
                    uid = make_userid()
                    mydb(f"insert into user values({uid},'{username}','{password}')")
                    print('注册成功')
                else:
                    print("密码不合法")
            else:
                print("该用户已存在")
        else:
            print("用户名不合法")
    # 登录
    elif shu == '2':
        username02 = input('请输入用户名')
        data = mydb(f"select uname from user where uname = '{username02}';")
        if len(data) == 0:
            print('此账号没有注册，请注册')
        else:
            password02 = input("请输入密码")
            data = mydb(f"select upassword from user where uname = '{username02}';")
            data = str(data[0][0])
            if password02 == data:
                print("登陆成功")
            else:
                print("密码错误")

    # 修改密码
    elif shu == '3':
        username03 = input('请输入用户名')
        data = mydb(f"select uname from user where uname = '{username03}';")
        if len(data) == 0:
            print('此账号没有注册无法修改')
        else:
            password03 = input("请输入旧密码")
            data = mydb(f"select upassword from user where uname = '{username03}';")
            data = str(data[0][0])
            if password03 == data:
                password = input("请输入新密码")

                if check_pwd(pwd=password):
                    mydb(f"update user set upassword='{password}' where uname = '{username03}';")
                    print('修改成功')
                elif password == password03:
                    print("新密码与旧密码一致")
                else:
                    print("密码不合法")
            else:
                print("密码错误")

    # 删除用户
    elif shu == '4':
        username04 = input('请输入用户名')
        data = mydb(f"select uname from user where uname = '{username04}';")
        if len(data) == 0:
            print('此账号没有注册无法删除')
        else:
            password03 = input("请输入密码")
            data = mydb(f"select upassword from user where uname = '{username04}';")
            data = str(data[0][0])
            if password03 == data:
                mydb(f"delete from user where uname = '{username04}';")
                print("删除成功")
            else:
                print("密码错误")
    else:
        print('输入的指令有误')